<?php
/**
 * Created by jinqiang on 2024/6/4 15:37.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Controllers\Banshi;


use App\Http\Controllers\Controller;
use App\Http\Services\Banshi\SizeService;
use Illuminate\Http\Request;

class SizeController extends Controller
{
    public function list(Request $request, SizeService $sizeService)
    {
        $this->validate($request, [
            'type' => 'required'
        ]);
        $param = $request->all();
        $res = $sizeService->search($param);
        return result($res);
    }

    public function set(Request $request, SizeService $sizeService)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $param = $request->all();
        $data = [];
        if($param['s_title'] ?? ""){
            $data = [
                "s_title" => $param['s_title'],
                "s_key" => $param['s_title']
            ];
        }
        if(isset($param['status']) && $param['status'] != ""){
            $data = ['status' => (Int)$param['status']];
        }
        if(empty($data)){
            return error("内容不能为空");
        }
        $res = $sizeService->save($param['id'], $data);
        return $res['code'] === 0 ? success("修改成功") : error($res['msg'], $res);
    }

    public function add(Request $request, SizeService $sizeService)
    {
        $this->validate($request, [
            'type' => 'required',
            's_title' => 'required'
        ]);
        $param = $request->all();
        $data = [
            'type' => (Int)$param['type'],
            's_title' => $param['s_title'],
            'createtime' => date('Y-m-d H:i:s')
        ];
        $res = $sizeService->create($data);
        return $res['code'] === 0 ? success("创建成功") : error($res['msg'], $res);
    }
}
